package smallChanges.statements;

import java.io.Reader;

import net.sourceforge.pmd.ast.ASTCompilationUnit;
import net.sourceforge.pmd.ast.JavaCharStream;
import net.sourceforge.pmd.ast.JavaParser;
import smallChanges.util.SCLogger;

public class ParseUtilities
{

	/**
	 * @param rdr
	 * @param sourceVersion
	 * @return
	 */
	public static ASTCompilationUnit parseToASTCompilationUnit(Reader rdr, String sourceVersion)
	{
		final JavaCharStream jcs = new JavaCharStream(rdr);
		final JavaParser parser = new JavaParser(jcs);
	
		if (sourceVersion.equals("1.3")) {
		    SCLogger.getFileLogger().trace("creating 1.3 parser");
		    parser.setJDK13();
		}
		else if (sourceVersion.equals("1.4")) {
		    SCLogger.getFileLogger().trace("creating 1.4 parser");
		}
		else if (sourceVersion.equals("1.5")) {
		    SCLogger.getFileLogger().trace("creating 1.5 parser");
		    parser.setJDK15();
		}
		else {
		    SCLogger.getFileLogger().fatal("ERROR: source version '" + sourceVersion + "' not recognized");
		    // TODO Mark this transaction as being an unparseable source version
		    System.exit(-1);
		}
	
		final ASTCompilationUnit cu    = parser.CompilationUnit();
		return cu;
	}

}
